#pragma once

#include <string>
#include <mysql/mysql.h>
#include "util.hpp"
#include "connect.hpp"
#include "result.hpp"

namespace easymysql
{
class executer
{
public:
    executer(MYSQL* handler) : _conn(handler)
    {}

    executer(const connection& conn) : _conn(conn.handler())
    {}

    template<typename... Args>
    result operator()(const std::string& query, Args&&... args)
    {
        return execute(query, std::forward<Args>(args)...);
    }

    template<typename... Args>
    result execute(const std::string& query, Args&&... args)
    {
        inquire(fmtr::format(query, std::forward<Args>(args)...));
        return result(_conn);
    }

private:
    void inquire(const std::string& query)
    {
        if (mysql_query(_conn, query.c_str()))
        {
            throw except(std::string("execute failed: ") + mysql_error(_conn));
        }
    }

private:
    MYSQL* _conn;
};
} // namespace easymysql
